package com.njrsrc.common.parser.sax;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.njrsrc.android.bean.IndustryInfo;

public class MySaxParser extends DefaultHandler {
	private List<IndustryInfo> industryList;
	private IndustryInfo industryInfo;

	public List<IndustryInfo> getIndustryList(InputStream inputStream) throws Exception {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		MySaxParser handler = new MySaxParser();
		parser.parse(inputStream, handler);
		return handler.getIndustryList();
	}

	public List<IndustryInfo> getIndustryList() {
		return industryList;
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if ("rows".equals(qName)) {
			industryList.add(industryInfo);
		}
	}

	@Override
	public void startDocument() throws SAXException {
		industryList = new ArrayList<IndustryInfo>();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if ("rows".equals(qName)) {
			industryInfo = new IndustryInfo();
			industryInfo.setCode(attributes.getValue(0));
			industryInfo.setName(attributes.getValue(1));
		}
	}
}
